package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckSafe;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.RepeatSubmit;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.util.DateUtil;
import com.vcc.data.entity.Notice;
import com.vcc.service.boss.request.notice.*;
import com.vcc.service.boss.response.notice.NoticeVoBoss;
import com.vcc.service.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    @RestPack
    @SaCheckPermission(value = "notice:list:create")
    @PostMapping("addNotice")
    @SaCheckLogin
    @RepeatSubmit
    public Boolean addNotice(
            @RequestBody @Validated AddNoticeDto addNoticeDto
    ) {
        if (noticeService.countHasPopup() > 5 && addNoticeDto.getHasPopup()) {
            throw new ProgramException("最多只能有5个弹窗公告");
        }
        Notice notice = new Notice();
        notice.setTitle(addNoticeDto.getTitle());
        notice.setContent(addNoticeDto.getContent());
        notice.setStartDate(DateUtil.parse(addNoticeDto.getStartDate()));
        notice.setEndDate(DateUtil.parse(addNoticeDto.getEndDate()));
        notice.setType(addNoticeDto.getType());
        notice.setStatus(false);
        notice.setHasPopup(addNoticeDto.getHasPopup());
        noticeService.save(notice);
        return true;
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:view")
    @GetMapping("paging")
    @SaCheckLogin

    public Page<NoticeVoBoss> paging(
            Integer pageNumber,
            Integer pageSize,
            Integer type,
            String title,
            Boolean status,
            Boolean hasPopup
    ) {
        Page<Notice> paging = noticeService.paging(
                Page.of(pageNumber != null ? pageNumber : 1, pageSize != null ? pageSize : 20)
                , title, type, status, hasPopup);
        return paging.map(NoticeVoBoss::new);
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:view")
    @GetMapping("getLastNotice")
    public NoticeVoBoss getLastNotice(
    ) {
        Page<Notice> page = new Page<>(1, 1);
        Page<Notice> paging = noticeService.paging(page, null, null, true, null);
        Notice notice = paging.getRecords().stream().findFirst().orElseThrow(() -> new NotFoundException("并没有公告"));
        return new NoticeVoBoss(notice);
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:edit")
    @PostMapping("updateStatus")
    @SaCheckSafe("NOTICE_EDIT")
    @SaCheckLogin
    public Boolean updateStatus(
            @RequestBody @Validated UpdateNoticeStatusDto updateNoticeStatusDto
    ) {
        if (updateNoticeStatusDto.getStatus() && noticeService.countHasPopup() > 5) {
            throw new ProgramException("最多只能有5个弹窗公告");

        }
        return noticeService.updateStatus(updateNoticeStatusDto);
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:edit")
    @PostMapping("updateHasPopup")
    @SaCheckSafe("NOTICE_EDIT")
    @SaCheckLogin
    public Boolean updateHasPopup(
            @RequestBody @Validated UpdateNoticeHasPopupDto updateNoticeHasPopupDto
    ) {
        if (updateNoticeHasPopupDto.getHasPopup() && noticeService.countHasPopup() > 5) {
            throw new ProgramException("最多只能有5个弹窗公告");
        }
        return noticeService.updateHasPopup(updateNoticeHasPopupDto.getId(), updateNoticeHasPopupDto.getHasPopup());
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:edit")
    @PostMapping("update")
    @SaCheckSafe("NOTICE_EDIT")
    @SaCheckLogin
    public Boolean update(
            @RequestBody @Validated UpdateNoticeDto updateNoticeDto
    ) {
        return noticeService.updateNotice(updateNoticeDto);
    }

    @RestPack
    @SaCheckPermission(value = "notice:list:remove")
    @PostMapping("remove")
    @SaCheckSafe("NOTICE_REMOVE")
    @SaCheckLogin
    public Boolean remove(
            @RequestBody @Validated RemoveNoticeDto removeNoticeDto
    ) {
        Notice notice = noticeService.getByIdOpt(removeNoticeDto.getId()).orElseThrow(() -> new NotFoundException("没有当前公告"));
        if (notice.getStatus()) {
            throw new ProgramException("当前公告已上架无法删除");
        }
        return noticeService.removeById(notice.getId());

    }


}
